<template>
	<view class="withdrawal">
		<u-navbar title="分润明细" bgColor="#1678ff" class="nav"
			:placeholder="true"
			:fixed="true"
			:safeAreaInsetTop="true">
			<view slot="left">
				<u-icon name="arrow-left" color="#fff" size="12" @click="onClickBack"></u-icon>
			</view>
			<view slot="right" @click="onClickRight">
				筛选
			</view>
		</u-navbar>
		<u-popup :show="filterTimer" @close="closeFilterTimer" mode="top" :safeAreaInsetTop="true">
			<view class="filterTimer">
				
				<view class="filterContent">
					<view class="w-flex w-flex-between">
						<view class="startTime" @click="showTime = true" :style="showCurrStartTime !== '请选择日期'?'color:#000;font-size:14px;':''">
							<text>{{showCurrStartTime}}</text>
							<u-icon name="arrow-down" color="#b3b3b3" size="14"></u-icon>
						</view>
						<text style="margin: 0 10px;">至</text>
						<view class="startTime" @click="showEndTime = true" :style="showCurrEndTime !== '请选择日期'?'color:#000;font-size:14px;':''">
							<text>{{showCurrEndTime}}</text>
							<u-icon name="arrow-down" color="#b3b3b3" size="14"></u-icon>
						</view>
					</view>
					<view>
						<u-datetime-picker
							:show="showTime"
							:value="startTime"
							mode="date"
							@cancel="cancel"
							@confirm="confirmTime"
							:maxDate="maxDate"
							:formatter="formatter"
						></u-datetime-picker>
						<u-datetime-picker
							:show="showEndTime"
							:value="endTime"
							mode="date"
							@cancel="cancelEndTime"
							@confirm="confirmEndTime"
							:maxDate="maxDate"
							:formatter="formatter"
						></u-datetime-picker>
					</view>
					<view class="w-flex w-flex-between w-mt-15 timerChoice">
						<text class="dateStyle" :class="activeDate === 0?'activeDate':''" @click="timerToday">今日</text>
						<text class="dateStyle" :class="activeDate === 1?'activeDate':''" @click="timerWeek">近一周</text>
						<text class="dateStyle" :class="activeDate === 2?'activeDate':''" @click="timerMonth">近一月</text>
					</view>
					<view class="w-mt-15">
						<view>
							交易类型
						</view>
						<view class="w-mt-15 w-flex w-flex-between">
							<u-checkbox-group
								v-model="MerchantStatus"
								placement="row"
								@change="MerchantStatusChange"
								style="display: flex;flex-wrap: wrap;width: 100%;"
							>
								<u-checkbox
									v-for="(item, index) in MerchantStatusList"
									:key="index"
									:label="item.name"
									:name="item.id"
									style="width: 25%;"
									size="14"
									labelSize="12"
									labelColor="#7e7e7e"
								>
								</u-checkbox>
							</u-checkbox-group>
						</view>
					</view>
					
				</view>
				<view class="w-flex w-flex-between w-mt-20 bottomBtn">
					<text class="txtBtn" @click="closeFilterTimer">取消</text>
					<text class="txtBtn" style="color: #1678ff;" @click="dateFilter">确定</text>
				</view>
			</view>
		</u-popup>
		
		<view class="w-content">
			<view class="titleBg">
				<view class="w-flex w-font-14">
					<!-- <text class="w-mr-15" @click="changeDate">{{choiceDate}}</text>
					<u-icon name="arrow-down-fill" color="#fff" size="10" @click="changeDate"></u-icon> -->
					
				</view>
				<view class="w-flex w-flex-between w-mt-15 w-font-12 profit">
					<text>成功交易：{{objData.summoney || 0}}</text>
					<text>累计分润：{{objData.cashmoneysum || 0}}</text>
				</view>
				<!-- <view class="w-font-12 w-mt-10 profit">
					自有分润：{{objData.userrunmoney || 0}}
				</view> -->
			</view>
			<u-datetime-picker
				:show="showchoiceDate"
				:value="choiceDialogDate"
				mode="date"
				@cancel="cancelchoiceDate"
				@confirm="confirmchoiceDate"
				:maxDate="choicemaxDate"
				:formatter="formatter"
			></u-datetime-picker>
		</view>
		<view>
			<view class="cu-list menu-avatar">
				<view class="cu-item cur"  v-for="(item, index) in detailedList" :key="index">
					<view class="cu-avatar radius lg">
						<view>
							<u-avatar :src="item.avatar?item.avatar:'../../static/icon/logo.png'" size="48" shape="square"></u-avatar>
						</view>
					</view>
					<view class="content">
						<view>
							<view class="text-cut">订单号：{{item.order_id || '无'}}</view>
						</view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">{{format(item.createtime*1000) || '无'}}</view></view>
					</view>
					<view class="action">
						<view class="main-color text-xl">{{item.money || 0}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="listItem" v-if="detailedList.length === 0">
			<u-empty
				mode="data"
				icon="http://cdn.uviewui.com/uview/empty/data.png"
			>
			</u-empty>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-loadmore :status="status" class="w-pb-10" v-if="detailedList.length >= 10"/>
	</view>
</template>

<script>
import {getPreMonth, format} from  "@/util/common.js"
export default {
	data() {
		return {
			filterTimer: false,
			showTime: false,
			showEndTime: false,
			startTime: Number(new Date().getTime() - 3600 * 1000 * 24 * 31),
			endTime: Number(new Date()),
			maxDate: Number(new Date()),
			showCurrEndTime: '请选择日期',
			showCurrStartTime: '请选择日期',
			MerchantStatus: [1, 10, 20, 30],
			MerchantStatusList: [
				{
					id: 1,
					name: '微信'
				},
				{
					id: 10,
					name: '支付宝'
				},
				{
					id: 20,
					name: '银联扫码'
				},
				{
					id: 30,
					name: 'POS刷卡'
				}
			],
			choiceDate: '2021年11月',
			showchoiceDate: false,
			choiceDialogDate: Number(new Date()),
			choicemaxDate: Number(new Date()),
			activeDate: 2,
			detailedList: [],
			format,
			objData: {},
			timerType: 0,
			status: 'loadmore',
			page: 1,
			closeRequest: false
		}
	},
	onLoad() {
		this.choiceDate = this.formatTimer(this.choiceDialogDate, 'YMD')
		this.showCurrStartTime =  getPreMonth(1);
		this.showCurrEndTime = this.formatTimer(Number(new Date()))
		this.initDataList(this.page)
		
	},
	onReachBottom () {
		if (this.detailedList.length >= 10) {
			this.status = 'loading';
			this.page = ++ this.page;
			setTimeout(() => {
				this.initDataList(this.page)
				if (this.closeRequest) this.status = 'nomore';
				else this.status = 'loading';
			}, 1500)
		}
	},
	methods: {
		async initDataList (page) {
			if (this.closeRequest) {
				this.$refs.uToast.show({
					type: 'default',
					title: '默认主题',
					message: "没有更多了",
				})
				this.status = 'nomore'
				return
			}
			let params = {
				agentid: uni.getStorageSync("YNF_INFO").user_id,
				start_time: this.showCurrStartTime + ' '+ '00:00:00',
				end_time: this.showCurrEndTime + ' '+ '23:59:59',
				pay_type: this.MerchantStatus,
				page: page,
				size: 10
			}
			const res = await this.$API({url: '/api/index/money_log', method:"POST", data: params})
			if (res.code === 1) {
				this.objData = res.data
				if (res.data.list.length > 0) {
					for (let item of res.data.list) {
						this.detailedList.push(item)
					}
					this.status = 'loadmore'
				} else {
					if (this.page > 1) {
						this.status = 'nomore'
						this.$refs.uToast.show({
							type: 'default',
							title: '默认主题',
							message: "没有更多了",
						})
						this.closeRequest = true
					}
				}
			}
		},
		// 返回页面
		onClickBack () {
			uni.navigateBack()
		},
		// 筛选日期
		onClickRight(){
			this.filterTimer = true
		},
		cancel () {
			this.showTime = false
		},
		cancelEndTime () {
			this.showEndTime = false
		},
		confirmEndTime (date) {
			this.showEndTime = false
			this.showCurrEndTime = this.formatTimer(date.value)
		},
		confirmTime (date) {
			this.showCurrStartTime = this.formatTimer(date.value)
			this.showTime = false
		},
		formatter(type, value) {
			if (type === 'year') {
					return `${value}年`
			}
			if (type === 'month') {
					return `${value}月`
			}
			if (type === 'day') {
					return `${value}日`
			}
			return value
		},
		formatTimer(time, type) {
			var time = new Date(time);
			var y = time.getFullYear();
			var m = time.getMonth()+1;
			var d = time.getDate();
			var h = time.getHours();
			var mm = time.getMinutes();
			var s = time.getSeconds();
			// if (type === 'YMD') {
			// 	return y+'年'+this.add0(m)+'月'+this.add0(d)+'日';
			// }
			return y+'-'+this.add0(m)+'-'+this.add0(d);
		},
		add0(m){return m<10?'0'+m:m },
		timerToday () {
			this.showCurrStartTime = this.formatTimer(Number(new Date()));
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.timerType = 0
			this.activeDate = 0
		},
		timerWeek () {
			this.showCurrStartTime = this.formatTimer(Number(new Date().getTime() - 3600 * 1000 * 24 * 7));
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.timerType = 1
			this.activeDate = 1
		},
		timerMonth () {
			this.showCurrStartTime = getPreMonth(1);
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.timerType = 2
			this.activeDate = 2
		},
		MerchantStatusChange (list) {
			this.MerchantStatus = list
			console.log(list)
		},
		closeFilterTimer () {
			this.filterTimer = false
		},
		dateFilter () {
			this.filterTimer = false
			this.detailedList = []
			this.page = 1
			this.closeRequest = false
			this.initDataList(this.page)
		},
		changeDate (){
			this.showchoiceDate = true
		},
		cancelchoiceDate () {
			this.showchoiceDate = false
		},
		confirmchoiceDate (date) {
			this.choiceDate = this.formatTimer(date.value, 'YMD')
			this.showchoiceDate = false
		},
		nonScanningCodeChange (list) {
			this.nonScanningCode = list
		}
	}
}
</script>

<style lang="scss" scoped>
.withdrawal {
	background: #FFFFFF;
	/deep/.u-navbar__content__title {
		color: #FFFFFF !important;
	}
	.nav {
		
		color: #FFFFFF;
		/deep/ .u-navbar__content {
			z-index: 20000;
		}
		/deep/.u-navbar__content__title {
			color: #FFFFFF;
		}
	}
	.filterTimer {
		background: #FFFFFF;
		
		.filterContent {
			padding: 20rpx 30rpx;
		}
		.startTime {
			background: #F1F1F1;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #b3b3b3;
			padding: 0 20rpx;
			width: 320rpx;
			height: 60rpx;
			border-radius: 10rpx;
			font-size: 12px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
	}
	/deep/ .u-popup {
		.u-slide-down-enter-active {
			top: 44px !important;
		}
	}
	.w-content {
		padding: 10rpx 20rpx;
		background: #1678ff;
		.titleBg {
			color: #FFFFFF;
			.profit {
				opacity: 0.9;
			}
		}
		
		
	}
	.listItem {
		background: #FFFFFF;
		padding: 10rpx 20rpx;
		
	}
}
.cu-list.menu-avatar>.cu-item .action {
	width: auto;
}
</style>
